package com.kzd.gtboon.jetpack.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.kzd.gtboon.app.AppFlowBus
import com.kzd.gtboon.app.SPSConstance
import com.kzd.gtboon.base.BaseViewModel
import com.kzd.gtboon.entity.*
import com.kzd.gtboon.jetpack.api.*
import com.kzd.gtboon.main.AccessAction
import com.module.lemlin.http.HttpResponseBody
import com.module.lemlin.http.HttpResponseDataT
import com.module.lemlin.http.SubscribeViewModel
import com.module.lemlin.owner.setRxLifeScope

class AccessViewModel(application: Application) : BaseViewModel(application) {

    lateinit var mAccessAction: AccessAction

    val accessGetCodeLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<String>>>()
    fun accessGetCode(phone: String, type: Int = 1) {
        setRxLifeScope(SubscribeViewModel(accessGetCodeLiveData)) {
            userMobileCode(phone, type)
        }
    }


    val accessUserLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<AccessUser>>>()
    private val accessUserSubscribe = object :
        SubscribeViewModel<HttpResponseDataT<AccessUser>>(accessUserLiveData) {
        override fun onSuccess(resp: HttpResponseDataT<AccessUser>) {
            super.onSuccess(resp)
            SPSConstance.uid = resp.data.uid
            SPSConstance.userInfo = resp.data.userInfo
            SPSConstance.userMine = resp.data.userMine
            AppFlowBus.post(IsLogin::class.java, IsLogin(isLogin = true))
        }
    }

    fun accessLoginVerify(phone: String, code: String) {
        setRxLifeScope(accessUserSubscribe) {
            val sessionId = accessGetCodeLiveData.value?.data?.data ?: ""
            appLoginVerify(phone, code, sessionId)
        }
    }


}